package cn.hhchat.hutool_re.core.util;

import cn.hhchat.hutool_re.exception.IORuntimeException;
import cn.hhchat.hutool_re.exception.UtilException;
import cn.hhchat.hutool_re.io.FastByteArrayOutputStream;

import java.io.*;

/**
 * Created this one by HMH on 2017/12/27.
 */
public class ObjectUtil {

    @SuppressWarnings("unchecked")
    public static <T> T clone(T object)  {
        FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(byteOut);
            out.writeObject(object);
            out.flush();
            final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
            return (T) in.readObject();
        } catch (Exception e) {
            throw new UtilException(e);
        }finally {
            IoUtil.close(out);
        }
    }

}
